package com.itheima.a04objectdemo01;

import java.util.StringJoiner;

/***
 *@title User
 *@description <TODO description class purpose>
 *@author 12468
 *@version 1.0.0
 *@create 2024-11-27 23:35
 **/

/**
 * Cloneable:
 *  如果一个接口里面没有抽象方法
 *  表示当前的接口是一个标记行接口
 *  现在 Cloneable 表示一旦实现，那么当前类的对象就可以被克隆
 *  如果没有现实，当前类的对象不能被克隆
 */
public class User implements Cloneable {
    private int id;
    private String username;
    private String password;
    private String path;
    private int[] data;

    public User() {
    }

    public User(int id, String username, String password, String path, int[] data) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.path = path;
        this.data = data;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int[] getData() {
        return data;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "User{id=\{id}, username='\{username}', password='\{password}', path='\{path}', data=\{arrToString()}}";
    }

    public String arrToString() {
        StringJoiner joiner = new StringJoiner(", ", "[", "]");
        for (int datum : data) {
            joiner.add(String.valueOf(datum));
        }
        return joiner.toString();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 调用父类的 clone 方法
        // 相当于让 Java 帮我们克隆一个对象，并把克隆之后的对象返回出去


        // 对于引用数据类型需要在 clone 方法重写创建复制2
        int[] newDate = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            newDate[i] = data[i];
        }
        // 调用父类中的方法克隆对象
        User clone = (User) super.clone();
        // 因为父类中的克隆方法是浅克隆，替换克隆出来的对象中数组地址值值
        clone.data = newDate;

        return clone;
    }
}
